function windout = genWind(wind)
% This function generates a wind direction and speed given a wind direction
% and speed.
% INPUTS
    % wind: a struct containing speed and direction where speed is in the
    % input units and direction is an angle given as an integer where 1 is
    % true north and conversion to degrees is integer*360/length(wind)
% OUTPUTS
    % windout: a struct with the same properties as wind
% There is also a local function to get the probability matrix to allow for
% future expansion.
    
sin = wind.speed;
din = wind.direction;

% Get the probability matrices. Convention is for even numbers to be
% direction probability matrices and odd to be speed prob matrices
dirProbMat = probMatrix(1);
spdProbMat = probMatrix(2);

% Given current wind, calculate next wind
% NOTE that this is currently not general at all! Must use the probability
% matrices provided.
dirProbRow = dirProbMat(din,:);
spdProbRow = spdProbMat(sin,:);

dout = randsample(16,1,true,dirProbRow);
sout = randsample(10,1,true,spdProbRow);

windout = struct('direction',dout,'speed',sout);

end

function P = probMatrix(matNum)

switch(matNum)
    % Convention is to use odd case numbers for direction and even case
    % numbers for speed.
    case 1 % Direction probability matrix
        P = [0.25, 0.375,0.225,0,    0,    0,    0,0,0,0,0,0,0,0,0.05,0.1;...
             0.1,  0.35, 0.41, 0.09, 0,    0,    0,0,0,0,0,0,0,0,0,0.05;...
             0.05, 0.25, 0.45, 0.2,  0.05, 0,    0,0,0,0,0,0,0,0,0,0;...
             0,    0.15, 0.35, 0.25,  0.15, 0.1,  0,0,0,0,0,0,0,0,0,0;...
             0,    0.05, 0.2,  0.31, 0.29, 0.1,  0.05,0,0,0,0,0,0,0,0,0;...
             0,    0,    0,    0.2,  0.3,  0.1,  0.25,0.15,0,0,0,0,0,0,0,0;...
             0,    0,    0,    0.05, 0.2,  0.2,  0.15,0.25,0.1,0.05,0,0,0,0,0,0;...
             0,    0,    0,    0,    0,    0.03, 0.15,0.27,0.35,0.2,0,0,0,0,0,0;...
             0,    0,    0,    0,    0,    0,    0.01,0.09,0.4,0.35,0.15,0,0,0,0,0;...
             0,    0,    0,    0,    0,    0,    0,0,0.18,0.42,0.35,0.05,0,0,0,0;...
             0,    0,    0,    0,    0,    0,    0,0,0,0.2,0.5,0.3,0,0,0,0;...
             0,    0,    0,    0,    0,    0,    0,0,0,0,0.35,0.45,0.2,0,0,0;...
             0,    0,    0,    0,    0,    0,    0,0,0,0,0.15,0.3,0.4,0.1,0.05,0;...
             0,    0,    0,    0,    0,    0,    0,0,0,0,0,0.2,0.35,0.3,0.1,0.05;...
             0.05, 0,    0,    0,    0,    0,    0,0,0,0,0,0.1,0.2,0.35,0.2,0.1;...
             0.3,  0.2,  0.05, 0,    0,    0,    0,0,0,0,0,0,0.1,0.15,0.15,0.05];
            
    case 2 % Speed probability matrix
        P = [0.7053, 0.2779, 0.0144, 0.0015, 0.0008, 0.0001, 0,      0,      0,      0;
             0.2405, 0.6089, 0.1306, 0.0153, 0.0041, 0.0005, 0,      0.0001, 0,      0;
             0.0256, 0.2839, 0.5317, 0.1352, 0.0178, 0.0048, 0.0005, 0.0003, 0.0002, 0;
             0.0042, 0.0491, 0.3116, 0.4954, 0.1191, 0.0179, 0.0023, 0.0003, 0,      0;
             0.0008, 0.0176, 0.0865, 0.3486, 0.4311, 0.0978, 0.0168, 0.0008, 0,      0;
             0,      0.0089, 0.0266, 0.1197, 0.3437, 0.4013, 0.0865, 0.0111, 0.0022, 0;
             0,      0.0152, 0.0076, 0.0455, 0.1212, 0.3561, 0.3485, 0.1061, 0,      0;
             0,      0,      0,      0.0526, 0,      0.2105, 0.3684, 0.2632, 0.0789, 0.0263;
             0,      0,      0,      0,      0,      0,      0.1818, 0.3636, 0.3636, 0.0909;
             0,      0,      0,      0,      0,      0,      0,      0,      1,      0];
    otherwise
        error('Unknown probability matrix in genWind')
end
end